package com.lc.pianostudent;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.util.Log;


import com.lc.pianostudent.db.BaseDB;
import com.lc.pianostudent.dialog.ShowDialog;
import com.lc.pianostudent.util.SystemUtil;
import com.lc.pianostudent.wxapi.WeiXinPay;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.zcx.helper.app.AppApplication;
import com.zcx.helper.app.AppInit;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.Http;

import org.json.JSONArray;

import java.io.File;

/**
 * Created by Administrator on 2016/3/14.
 */
@AppInit(name = "PianoStudent", log = true, initialize = false, width = 720, scale = 1f)
public class BaseApplication extends AppApplication {

    public static com.lc.pianostudent.BasePreferences BasePreferences;


    public static BaseDB baseDB;
    public static JSONArray jsonArray;//彩票确认提交拼接jsonArray
    public static WeiXinPay WeiXinPay;
    // 默认存放图片的路径
    public final static String DEFAULT_SAVE_IMAGE_PATH = Environment.getExternalStorageDirectory() + File.separator + "CircleDemo" + File.separator + "Images"
            + File.separator;

    public static int mKeyBoardH = 0;
    private static Context mContext;

    @Override
    public void onCreate() {

        super.onCreate();

        BasePreferences = new BasePreferences(this, "PianoStudent");
        showSystemParameter();

        baseDB = new BaseDB();

        jsonArray = new JSONArray();
        WeiXinPay = new WeiXinPay(this);
        mContext = getApplicationContext();
        initImageLoader();

        Http.getInstance().setOnStartEndCreateDialog(new Http.OnStartEndCreateDialog() {
            @Override
            public void show(Dialog dialog) throws Exception {
                dialog.show();
            }

            @Override
            public void dismiss(Dialog dialog) throws Exception {
                dialog.dismiss();
            }

            @Override
            public Dialog create(Context context) throws Exception {
                ShowDialog showDialog = new ShowDialog(context);
                showDialog.setCancelable(false);

                return showDialog;
            }
        });

        GlideLoader.getInstance().init(R.mipmap.dongtai);

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    public static Context getContext(){
        return mContext;
    }

    /** 初始化imageLoader */
    private void initImageLoader() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .cacheInMemory(true)
                //.cacheOnDisk(true)默认不缓存硬盘，以免本地图片加载也被缓存
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .memoryCache(new WeakMemoryCache())
                .threadPoolSize(4)
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheFileCount(10)
                .diskCache(new UnlimitedDiskCache(new File(DEFAULT_SAVE_IMAGE_PATH)))
                .defaultDisplayImageOptions(options)
                .build();
        ImageLoader.getInstance().init(config);
    }
    private void showSystemParameter() {
        String TAG = "系统参数：";
        Log.e(TAG, "手机厂商：" + SystemUtil.getDeviceBrand());
        Log.e(TAG, "手机型号：" + SystemUtil.getSystemModel());
        Log.e(TAG, "手机当前系统语言：" + SystemUtil.getSystemLanguage());
        Log.e(TAG, "Android系统版本号：" + SystemUtil.getSystemVersion());

    }
}
